# coding=utf8
from app.extensions import db
from app.modules.common.utils import JsonEncoder
from app.modules.common.dbUtil import createId
from sqlalchemy import func


class IntegrationInfo(db.Model, JsonEncoder):
    __tablename__ = "integration"
    integration_id = db.Column(db.String, primary_key = True)  # 集成计划id
    integration_year = db.Column(db.Integer)
    integration_cycle = db.Column(db.Integer)  # 集成计划周期  第一周  第二周等
    integration_name = db.Column(db.String)  # 集成计划名
    integration_date = db.Column(db.Date)  # 集成日期
    online_date = db.Column(db.Date)  # 上线日期
    integration_status = db.Column(db.String)
    integration_desc = db.Column(db.String)
    isEmergency = db.Column(db.String)
    members = None
    projects = None

    def __init__(self, integration_cycle = None, integration_name = None, integration_date = None, online_date = None,
                 integration_status = None, integration_year = None, integration_desc = None, isEmergency = '1'):
        self.integration_id = createId('ig')
        self.integration_year = integration_year
        self.integration_cycle = integration_cycle
        self.integration_name = integration_name
        self.integration_date = integration_date
        self.online_date = online_date
        self.integration_status = integration_status
        self.integration_desc = integration_desc
        self.isEmergency = isEmergency

    def to_dict(self):
        return {
            'integration_id': self.integration_id,
            'integration_year': self.integration_year,
            'integration_cycle': self.integration_cycle,
            'integration_name': self.integration_name,
            'integration_date': self.integration_date.strftime('%Y-%m-%d') if self.integration_date else None,
            'online_date': self.online_date.strftime('%Y-%m-%d') if self.online_date else None,
            'integration_status': self.integration_status,
            'integration_desc': self.integration_desc,
            'isEmergency': self.isEmergency,
        }


class IntegrationEditionChengeRecord(db.Model, JsonEncoder):
    __tablename__ = "change_record"
    record_id = db.Column(db.String, primary_key = True)
    integration_id = db.Column(db.String)
    integration_name = None
    plan_id = db.Column(db.String)
    plan_name = None
    create_date = db.Column(db.DateTime, default = func.now())
    user_id = db.Column(db.String)
    user_realname = db.Column(db.String)
    user_role = db.Column(db.String)
    trunk_name = db.Column(db.String)
    old_project = db.Column(db.String)
    old_edition = db.Column(db.String)
    old_status = db.Column(db.String)
    new_project = db.Column(db.String)
    new_edition = db.Column(db.String)
    new_status = db.Column(db.String)
    is_after_hand = db.Column(db.String)
    key = db.Column(db.String)
    integration_status = db.Column(db.String)

    def __init__(self, integration_id = None, plan_id = None, user_id = None, user_role = None,
                 user_realname = None, trunk_name = None, old_project = None, old_edition = None, old_status = None,
                 new_project = None, new_edition = None, new_status = None, is_after_hand = '1', key = None,
                 integration_status = None):
        self.record_id = createId('ic')
        self.integration_id = integration_id
        self.plan_id = plan_id
        self.user_id = user_id
        self.user_realname = user_realname
        self.user_role = user_role
        self.trunk_name = trunk_name
        self.old_project = old_project
        self.old_edition = old_edition
        self.old_status = old_status
        self.new_project = new_project
        self.new_edition = new_edition
        self.new_status = new_status
        self.is_after_hand = is_after_hand
        self.key = key
        self.integration_status = integration_status

    def to_dict(self):
        return {
            'record_id': self.record_id,
            'integration_id': self.integration_id,
            'integration_name': self.integration_name,
            'plan_id': self.plan_id,
            'plan_name': self.plan_name,
            'create_date': self.create_date.strftime('%Y-%m-%d %H:%M:%S') if self.create_date else None,
            'user_id': self.user_id,
            'user_realname': self.user_realname,
            'user_role': self.user_role,
            'trunk_name': self.trunk_name,
            'old_project': self.old_project,
            'old_edition': self.old_edition,
            'old_status': self.old_status,
            'new_project': self.new_project,
            'new_edition': self.new_edition,
            'new_status': self.new_status,
            'is_after_hand': self.is_after_hand,
            'key': self.key,
            'integration_status': self.integration_status,
        }


class IntegrationPlan(db.Model, JsonEncoder):
    __tablename__ = "integration_plan"
    integration_id = db.Column(db.String, primary_key = True)  # 集成计划名
    integration_name = None
    plan_id = db.Column(db.String, primary_key = True)
    plan_name = None

    def __init__(self, integration_id, plan_id):
        self.integration_id = integration_id
        self.plan_id = plan_id

    def to_dict(self):
        return {
            'integration_id': self.integration_id,
            'integration_name': self.integration_name,
            'plan_id': self.plan_id,
            'plan_name': self.plan_name,
        }


class IntegrationOnlineDesc(db.Model, JsonEncoder):
    __tablename__ = "integration_online_desc"
    id = db.Column(db.String, primary_key = True)
    integration_id = db.Column(db.String)
    create_date = db.Column(db.Date, default = func.now())
    update_date = db.Column(db.Date, default = func.now())
    user_id = db.Column(db.String)
    real_name = db.Column(db.String)
    desc = db.Column(db.String)
    index = db.Column(db.Integer)
    file_name = db.Column(db.String)
    file_path = db.Column(db.String)

    def __init__(self, integration_id = None, update_date = None, user_id = None, real_name = None,
                 desc = None, index = None,file_name=None,file_path=None):
        self.id = createId('iod')
        self.integration_id = integration_id
        self.update_date = update_date
        self.user_id = user_id
        self.real_name = real_name
        self.desc = desc
        self.index = index
        self.file_name = file_name
        self.file_path = file_path

    def to_dict(self):
        return {
            'id': self.id,
            'integration_id': self.integration_id,
            'create_date': self.create_date.strftime('%Y-%m-%d %H:%M:%S') if self.create_date else None,
            'update_date': self.update_date.strftime('%Y-%m-%d %H:%M:%S') if self.update_date else None,
            'user_id': self.user_id,
            'real_name': self.real_name,
            'desc': self.desc,
            'index': self.index,
            'file_name': self.file_name,
            'file_path': self.file_path,
        }
